package com.zbbt.sportslottery.ui.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;

import com.gc.materialdesign.views.ButtonRectangle;
import com.gc.materialdesign.views.ProgressBarCircularIndetermininate;
import com.google.gson.JsonObject;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.app.ServerConfig;
import com.zbbt.sportslottery.net.TypeResultHttpHandler;
import com.zbbt.sportslottery.net.ZbHttpClient;
import com.zbbt.sportslottery.ui.widget.ClearEditText;
import com.zbbt.sportslottery.util.DataValidator;
import com.zbbt.sportslottery.util.LogUtil;
import com.zbbt.sportslottery.util.Md5;
import com.zbbt.sportslottery.util.SharePreferUtil;
import com.zbbt.sportslottery.util.ToastUtil;
import com.zbbt.sportslottery.util.UmengUtil;
/**
 * 
 * @ClassName: ForgetPwdActivity
 * @Description: 忘记密码
 * @author: yangwenquan
 * @date: 2015年3月3日 下午4:34:36     
 * @version: V1.0
 */
public class ForgetPwdActivity extends SimpleBaseActivity {
	private ClearEditText userName;
	private ClearEditText userPassword;
	private ButtonRectangle registerButton;
	private ClearEditText phonecode;
	private ButtonRectangle getcode;
	private String usernameContent;
	private String passwordContent;
	private String codeContent;
	private ProgressBarCircularIndetermininate mProgressBar;
	boolean isHttp = true;// 是否执行完一次网络连接

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
		setContentView(R.layout.activity_new_pwd);
	}

	@Override
	protected void initView() { 
		mProgressBar = (ProgressBarCircularIndetermininate) findViewById(R.id.mProgressBar);
		userName = (ClearEditText) findViewById(R.id.username_edittext_register);
		userPassword = (ClearEditText) findViewById(R.id.password_edittext_register);
		phonecode = (ClearEditText) findViewById(R.id.phonecode_edittext_register);
		getcode = (ButtonRectangle) findViewById(R.id.phonecode_edittext_btn);
		getcode.setTextSize(17);
		getcode.setTextColor(getResources().getColor(R.color.white));
		registerButton = (ButtonRectangle) findViewById(R.id.register_button);
		registerButton.setTextColor(getResources().getColor(R.color.white));
		registerButton.setTextSize(20);
	}

	@Override
	protected void setListener() {
		getcode.setOnClickListener(this);
		registerButton.setOnClickListener(this);
	}

	@Override
	protected void onClickEvent(View v) {
		switch (v.getId()) {
		case R.id.phonecode_edittext_btn:// 获取验证码
			requestCodeTask();

			break;
		case R.id.register_button:// 确认提交
			registeronClick();
			break;
		default:
			break;
		}
	}

	/**
	 * 
	 * @MethodName:registeronClick
	 * @Description: 确认验证
	 * @author：GaomingShuo
	 * @date：2015-3-2 下午7:26:52 void
	 */
	public void registeronClick() {
		usernameContent = userName.getText().toString().trim();
		passwordContent = userPassword.getText().toString().trim();
		codeContent = phonecode.getText().toString().trim();
		LogUtil.printI(TAG, "账号" + usernameContent + "====密码" + passwordContent);

		if (TextUtils.isEmpty(usernameContent)) {
			ToastUtil.showShort("账号不能为空");
			userName.setShakeAnimation();
			return;
		}
		if (!DataValidator.isMobile(usernameContent)) {
			ToastUtil.showShort("请输入正确的手机号");
			userName.setShakeAnimation();
			return;
		}
		if (TextUtils.isEmpty(passwordContent)) {
			ToastUtil.showShort("密码不能为空");
			userPassword.setShakeAnimation();
			return;
		}
		if (passwordContent.length() > 16 || passwordContent.length() < 6) {
			ToastUtil.showShort("您输入的密码应在6~16个字符");
			userPassword.setShakeAnimation();
			return;
		}
		if (TextUtils.isEmpty(codeContent)) {
			ToastUtil.showShort("验证码不能为空");
			phonecode.setShakeAnimation();
			return;
		}
		if (isHttp) {
			httpData(usernameContent, passwordContent);
		}

	}

	/**
	 * 
	 * @MethodName:httpData
	 * @Description: 拿到符合要求的账号密码，请求网络连接
	 * @author：GaomingShuo
	 * @date：2015-3-2 下午7:27:45
	 * @param usernameContent2
	 * @param passwordContent2
	 *            void
	 */
	private void httpData(final String usernameContent, final String passwordContent) {
		isHttp = false;
		mProgressBar.setVisibility(View.VISIBLE);
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("mobile", usernameContent);
		jsonObject.addProperty("password",Md5.getMD5(passwordContent));
		jsonObject.addProperty("checkCode", codeContent);
		ZbHttpClient.postNewApi(ServerConfig.API_FORGETPWD, jsonObject, new TypeResultHttpHandler(true) {

			@Override
			public void onSuccess() {
				UmengUtil.onEvent(context, Constant.UMENG_PERSON_FORGETPAD);
				mProgressBar.setVisibility(View.GONE);
				ToastUtil.showShort("密码修改成功，请重新登录");
				SharePreferUtil.putString(Constant.USER_NAME, usernameContent);
				SharePreferUtil.putString(Constant.PASSWORD, passwordContent);
				isHttp = true;
				goActivity(LoginActivity.class);
				finish();
			}


			@Override
			public void onFailure(String errorCode, String errorMsg) {
				mProgressBar.setVisibility(View.GONE);
				isHttp = true;
				downTimer.cancel();
				getcode.setText("重新获取");
				getcode.setEnabled(true);
				LogUtil.printI("修改密码失败", errorMsg);
			}
		});
	}

	/**
	 * 
	 * @MethodName:requestCodeTask
	 * @Description: 语音验证信息验证
	 * @author：GaomingShuo
	 * @date：2015-3-2 下午7:25:31 void
	 */
	private void requestCodeTask() {

		usernameContent = userName.getText().toString().trim();
		if (TextUtils.isEmpty(usernameContent)) {
			ToastUtil.showShort("手机号不能为空");
			userName.setShakeAnimation();
			return;
		}
		if (!DataValidator.isMobile(usernameContent)) {
			ToastUtil.showShort("请输入正确的手机号");
			userName.setShakeAnimation();
			return;
		}
		mProgressBar.setVisibility(View.VISIBLE);
		downTimer.start();
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("mobile", userName.getText().toString());
		ZbHttpClient.postNewApi(ServerConfig.API_REGISTER_GETCODE, jsonObject, new TypeResultHttpHandler(true) {

			@Override
			public void onSuccess() {
				mProgressBar.setVisibility(View.GONE);
				ToastUtil.showLong("验证码获取成功,5分钟内有效,请等待客服电话告知");
			}


			@Override
			public void onFailure(String errorCode, String errorMsg) {
				ToastUtil.showLong(errorMsg);
				mProgressBar.setVisibility(View.GONE);
				downTimer.cancel();
				getcode.setText("重新获取");
				getcode.setEnabled(true);
				
			}
		});
	}

	@Override
	protected void initData() {

	}

	/**
	 * 倒计时60秒 发送短信
	 */
	CountDownTimer downTimer = new CountDownTimer(60000, 1000) {
		@Override
		public void onTick(long millisUntilFinished) {
			getcode.setText("" + millisUntilFinished / 1000);
			getcode.setEnabled(false);
		}

		@Override
		public void onFinish() {
			getcode.setText("重新获取");
			getcode.setEnabled(true);
		}
	};

}
